﻿using System;
using System.Collections.Generic;
using System.Linq;
using XPressify.Data.CompliedQueries;
using XPressify.Data.Helpers;

namespace XPressify.Data
{
    public partial class ExtendedRepository
    {
        public IEnumerable<User> GetFriendsOf(Guid userId)
        {
            return UserCompiledQueries.GetAllFriends(this as Repository ,userId);
        }

        public IEnumerable<User> GetFriendsOfFriendsOf(Guid userId)
        {
            IEnumerable<User> result = new List<User>();
            var friends = UserCompiledQueries.GetAllFriends(this as Repository, userId);
            return friends.Aggregate(result,(current, friend) => current.Union(GetFriendsOf(friend.Id).Where(friendsFriend => !friends.Contains(friendsFriend))));
        }

        public User GetAdminOf(Guid subjectId)
        {
            return UserCompiledQueries.GetAdmin(this, subjectId);
        }

        public IEnumerable<User> GetMembersOf(Guid subjectId)
        {
            return UserCompiledQueries.GetMembersOfOrderByName(this, subjectId);
        }

        public string GetProfilePicturePath(Guid userId)
        {
            string profilePicturePath = "";
            var profileAlbum = AlbumCompiledQueries.GetProfileAlbum(this, userId);
            profilePicturePath = "/Media/" + userId + "/" + profileAlbum.Id + "/" + "ProfilePicture";
            return profilePicturePath;
        }

        public List<Post> GetAllComments(Post post)
        {
            var allComments = new List<Post>();


            //Only with its comments
            if (post.CommentType == CommentType.PostWithItsComments)
            {
                allComments = PostCompiledQueries.GetAllCommentsLocal(this, post).ToList(); // all childrens posts
            }
            //comments from all post with same BaseObject // Global
            else if (post.CommentType == CommentType.PostWithGlobalComments)
            {
                allComments = PostCompiledQueries.GetAllCommentsGlobal(this, post).ToList();
            }

            return allComments;
        }
    }
}
